package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * TODO
 *
 * @Author katana
 * @Date 2025/5/20 19:51
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    
    
    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 查询当前用户,保证购物车数据是属于当前用户的
        BaseContext.getCurrentId();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> carts = shoppingCartMapper.list(shoppingCart);
        
        // 判断购物车数据是否已经存在
        if (carts != null && carts.size() > 0) {
            //  如果已经存在，则做数量+1处理
            ShoppingCart cart = carts.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //  如果不存在，则添加到购物车，默认数量为1
            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(shoppingCart);
        }
        
    }
    
    
    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }
    
    /**
     * 清空购物车商品
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
    
    /**
     * 购物车商品数量减一
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public RuntimeException sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        
        try {
            List<ShoppingCart> carts = shoppingCartMapper.list(shoppingCart);
            if (CollectionUtils.isEmpty(carts)) {
                return new RuntimeException("购物车为空，用户ID: " + shoppingCart.getUserId());
            }
            
            carts.forEach(cart -> {
                try {
                    if (cart.getNumber() == 1) {
                        shoppingCartMapper.deleteById(cart.getId());
                    } else {
                        cart.setNumber(cart.getNumber() - 1);
                        shoppingCartMapper.updateNumberById(cart);
                    }
                } catch (Exception e) {
                    throw new RuntimeException("更新购物车商品数量失败，商品ID: " + cart.getId(), e);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException("获取购物车列表失败，用户ID: " + shoppingCart.getUserId(), e);
        }
        
        return null;
    }
    
}
